import type { AxiosInstance } from "axios";
//响应拦截是是根据响应的状态码来进行拦截
export default (http: AxiosInstance) => {
    //采用链式调用的形式来调用拦截器,所以要想拦截器下走直接return
    http.interceptors.response.use((response) => {
        if (response.status == 200) {

            if (response.data?.status != 200) {
                if (response.data.status == 410) { 
                    return response
                }
                ElMessage.warning({ message: response.data?.message, center: true, grouping: true })
                
        }else {
            return response
        }
}}
        , (error) => {
        //Flag:超时处理
        let originalRequest = error.config;
        if (error.code == 'ECONNABORTED' && error.message.indexOf('timeout') != -1 && !originalRequest._retry) {
            ElMessage.error({
                message: '请求超时,请重试',
                center: true
            })
        }
        //Flag:404页面跳转
        if (error.response.status == 404) {
            ElMessage.error({
                message: '404 Not Found',
                center: true,
            })
        } else {
            ElMessage.error({
                message: '未知错误',
                center: true
            })
        }
        //Flag:错误后抛--也可以直接处理
        new Error(error.message)
    })
}